Thread: Adapt a singleton class to lose it`s singleton feature

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    121

    Adapt a singleton class to lose it`s singleton feature

    Hello,
    I have a class that is a singleton. However I need more instances of that class, so I am interested do I have to rewrite the class, or I can adapt it to loose that functionality whithout touch that class?
    Code:
    class MyClass : private SingletonClass 
    {
    ...
       MyClass m_instances[3]; 
    
    };
    I don`t want to touch the library class, just to add it an extra feat of not being only Singleton.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by heatblazer View Post
    Hello,
    I have a class that is a singleton. However I need more instances of that class, so I am interested do I have to rewrite the class, or I can adapt it to loose that functionality whithout touch that class?
    Code:
    class MyClass : private SingletonClass 
    {
    ...
       MyClass m_instances[3]; 
    
    };
    I don`t want to touch the library class, just to add it an extra feat of not being only Singleton.
    What possible reason could you have for wanting to inherit from a singleton?

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Without changing the class? No - or at least not by any sensible means.

    With minimal changes? Unlikely. You're changing a fairly core design assumption, one that may likely have impacted many areas of the original development. You'd have to really understand the original code well, and if you did that, you would already know the work it would take to make this no longer a singleton.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    121
    Let`s say I need two instances of that class form pjsua2: 4. Endpoint — PJSUA2 Documentation 1.0-alpha documentation

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by heatblazer View Post
    Let`s say I need two instances of that class form pjsua2: 4. Endpoint — PJSUA2 Documentation 1.0-alpha documentation
    In that case, pretty much the answer is no. The whole library looks to be designed around that being a singleton.

    I would question if you really needed more than one instance - there are good reasons why it's a singleton - and if you really had good reasons, you might need to look elsewhere for a library for your needs.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a singleton class
    By Absurd in forum C++ Programming
    Replies: 18
    Last Post: 06-18-2013, 07:57 PM
  2. Class vs. Namespace as Singleton
    By eros in forum C++ Programming
    Replies: 0
    Last Post: 03-28-2012, 06:07 AM
  3. Singleton Class error
    By anirban in forum C++ Programming
    Replies: 10
    Last Post: 10-28-2010, 06:44 AM
  4. Bug in Singleton class
    By marquito in forum C++ Programming
    Replies: 17
    Last Post: 12-14-2007, 01:02 PM
  5. singleton class problem
    By ... in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2003, 06:16 PM

Tags for this Thread